package com.ch.fastbuilder.Interceptor;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import com.ch.fastbuilder.annotation.DataSource;
import com.ch.fastbuilder.transaction.DynamicDataSourceHolder;

public class DataSourceInterceptor implements MethodInterceptor {

	public Object invoke(MethodInvocation invocation) throws Throwable {
		Method method = invocation.getMethod();
		
        if(method.isAnnotationPresent(DataSource.class)) {
        	DataSource data = method.getAnnotation(DataSource.class);
        	DynamicDataSourceHolder.setDataSource(data.value());
        }

       //执行被拦截的方法，切记，如果此方法不调用，则被拦截的方法不会被执行。
        return invocation.proceed();
	}
}
